package com.nuatar.meibi.pubvar;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.nuatar.meibi.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.widget.ImageView;

public class ntImageView extends ImageView {
	public ImagePair ip;
	public int position;
	public ntImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO 自动生成的构造函数存根
	}
	public ntImageView(Context context,ImagePair data,String id,int pos){
		super(context);
		ip=data;
		position=pos;
		this.setFocusable(false);
		this.setClickable(false);
		setImageId(id);
	}
	public void setImageId(String id){
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 1;
		String filePath=PhoneMaster.getFilePathFromId(id, ".ldt");
		if(ip!=null){
			
			byte[] bs;
			try {
				bs = PhoneMaster.getFileByteArray((new File(filePath)), ip.Seeker, ip.Length);
				InputStream is = new ByteArrayInputStream(bs); 
			
				Bitmap bmp=BitmapFactory.decodeStream(is, null, options);
				if(bmp!=null){
					this.setImageBitmap(Mysizer.CreateScaleBitmap(bmp));
				}else{
					this.setImageBitmap(Mysizer.CreateScaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.loads,options)));
				}
			} catch (FileNotFoundException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			
		}else{
			this.setImageBitmap(Mysizer.CreateScaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.loads,options)));
		}
		
	}
	@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // For simple implementation, or internal size is always 0.
        // We depend on the container to specify the layout size of
        // our view. We can't really know what it is since we will be
        // adding and removing different arbitrary views and do not
        // want the layout to change as this happens.
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
 
        // Children are just made to fill our space.
        int childWidthSize = getMeasuredWidth();
        int childHeightSize = getMeasuredHeight();
        //高度和宽度一样
        heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
